<template>
	<view class="login-container">
		<view class="login-header">
			<image src="/static/logo.png" mode="aspectFit" class="logo"></image>
			<text class="title">智慧零工</text>
		</view>
		
		<view class="login-form">
			<!-- 角色选择 -->
			<view class="role-select">
				<view class="role-title">请选择您的身份</view>
				<view class="role-options">
					<view 
						class="role-option" 
						:class="{ active: role === 'user' }"
						@click="role = 'user'"
					>
						<image src="/static/user-role.png" mode="aspectFit" class="role-icon"></image>
						<text>求职者</text>
					</view>
					<view 
						class="role-option" 
						:class="{ active: role === 'company' }"
						@click="role = 'company'"
					>
						<image src="/static/company-role.png" mode="aspectFit" class="role-icon"></image>
						<text>企业</text>
					</view>
				</view>
			</view>
			
			<!-- 手机号输入 -->
			<view class="input-group">
				<u-input
					v-model="phone"
					type="number"
					placeholder="请输入手机号"
					:border="true"
					prefixIcon="phone"
				></u-input>
			</view>
			
			<!-- 验证码输入 -->
			<view class="input-group">
				<u-input
					v-model="code"
					type="number"
					placeholder="请输入验证码"
					:border="true"
					prefixIcon="lock"
				>
					<template slot="suffix">
						<view class="code-btn" @click="getCode" :class="{ disabled: counting }">
							{{counting ? `${countdown}s后重试` : '获取验证码'}}
						</view>
					</template>
				</u-input>
			</view>
			
			<!-- 登录按钮 -->
			<view class="login-btn" @click="login">
				<text>登录</text>
			</view>
			
			<!-- 用户协议 -->
			<view class="agreement">
				<text>登录即代表您同意</text>
				<text class="link" @click="toAgreement">《用户协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { login, getCode } from '../../utils/api/modules/user';
	
	export default {
		data() {
			return {
				phone: '',
				code: '',
				role: 'user', // 默认选择求职者
				counting: false,
				countdown: 60
			}
		},
		methods: {
			// 获取验证码
			async getCode() {
				if (this.counting) return;
				
				// 验证手机号
				if (!/^1[3-9]\d{9}$/.test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				
				try {
					const res = await getCode(this.phone);
					if (res.code === 200) {
						uni.showToast({
							title: '验证码已发送',
							icon: 'success'
						});
						this.startCountdown();
					} else {
						uni.showToast({
							title: res.msg || '获取验证码失败',
							icon: 'none'
						});
					}
				} catch (error) {
					uni.showToast({
						title: '获取验证码失败',
						icon: 'none'
					});
				}
			},
			
			// 开始倒计时
			startCountdown() {
				this.counting = true;
				this.countdown = 60;
				const timer = setInterval(() => {
					this.countdown--;
					if (this.countdown <= 0) {
						clearInterval(timer);
						this.counting = false;
					}
				}, 1000);
			},
			
			// 登录
			async login() {
				// 验证手机号
				if (!/^1[3-9]\d{9}$/.test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				
				// 验证验证码
				if (!/^\d{6}$/.test(this.code)) {
					uni.showToast({
						title: '请输入6位验证码',
						icon: 'none'
					});
					return;
				}
				
				try {
					const res = await login({
						phone: this.phone,
						code: this.code,
						role: this.role
					});
					
					if (res.code === 200) {
						// 保存用户信息和token
						uni.setStorageSync('token', res.data.token);
						uni.setStorageSync('user', res.data.user);
						
						// 跳转到首页
						uni.reLaunch({
							url: '/pages/index/index'
						});
					} else {
						uni.showToast({
							title: res.msg || '登录失败',
							icon: 'none'
						});
					}
				} catch (error) {
					uni.showToast({
						title: '登录失败',
						icon: 'none'
					});
				}
			},
			
			// 跳转到用户协议
			toAgreement() {
				uni.navigateTo({
					url: '/pages/agreement/agreement'
				});
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		min-height: 100vh;
		background: #fff;
		padding: 40rpx;
	}
	
	.login-header {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 60rpx;
		margin-bottom: 80rpx;
		
		.logo {
			width: 160rpx;
			height: 160rpx;
			margin-bottom: 20rpx;
		}
		
		.title {
			font-size: 40rpx;
			font-weight: bold;
			color: #333;
		}
	}
	
	.login-form {
		.role-select {
			margin-bottom: 40rpx;
			
			.role-title {
				font-size: 28rpx;
				color: #666;
				margin-bottom: 20rpx;
			}
			
			.role-options {
				display: flex;
				justify-content: space-between;
				gap: 20rpx;
				
				.role-option {
					flex: 1;
					height: 160rpx;
					background: #f5f5f5;
					border-radius: 12rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					gap: 12rpx;
					
					&.active {
						background: #e6f7ff;
						border: 2rpx solid #1890ff;
					}
					
					.role-icon {
						width: 64rpx;
						height: 64rpx;
					}
					
					text {
						font-size: 28rpx;
						color: #333;
					}
				}
			}
		}
		
		.input-group {
			margin-bottom: 30rpx;
		}
		
		.code-btn {
			font-size: 28rpx;
			color: #1890ff;
			padding: 0 20rpx;
			
			&.disabled {
				color: #999;
			}
		}
		
		.login-btn {
			height: 88rpx;
			background: #1890ff;
			border-radius: 44rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 60rpx;
			
			text {
				color: #fff;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		
		.agreement {
			margin-top: 30rpx;
			text-align: center;
			font-size: 24rpx;
			color: #999;
			
			.link {
				color: #1890ff;
			}
		}
	}
</style> 